/******************************************************************

setup_expenditure.do

Subroutine to calculate expenditure variables for all analysis programs;

mt 20190506
*******************************************************************/
# delim ;  

* expenditure on length and IRI from SF12a. Check that categories are right. Can we break apart ROW?;

*conversion factor to get to 10^6 2010 dollars;
gen deflate= 1/(ppiaco_2010*10);
*gen deflate= 1;

*consolidate rural and urban SF12a variables that we need;
local rur_urb_list 	"exp_eng_row_IH 		exp_row_IH 		exp_eng_IH		exp_new_cons_IH
			exp_relocation_IH		exp_recons_IH		exp_recons_add_IH	exp_recons_noadd_IH
			exp_maj_wide_IH			exp_R3_IH       	exp_R3_min_wide_IH	exp_R3_rehab_rest_IH 	
			exp_R3_resurf_IH
			exp_new_bridge_IH exp_bridgereplacement_IH exp_majorbridgerehab_IH exp_minorbridgerehab_IH exp_bridge_IH
			";
foreach exp_var of local rur_urb_list{;
		replace `exp_var'_urban=cond(`exp_var'_urban==.,0,`exp_var'_urban);
		replace `exp_var'_rural=cond(`exp_var'_rural==.,0,`exp_var'_rural);
		gen `exp_var'_all = `exp_var'_urban + `exp_var'_rural;
		drop `exp_var'_urban `exp_var'_rural;
		};

*consolidate SF12a variables into nominal construction and resurfacing for pre and post 1998;
*NB: exp_new_cons_IH_all and exp_maj_wide_IH_all are common to pre and post 1998 construction;
gen exp_L_IH_SF12a_n_old=	exp_eng_row_IH_all + exp_new_cons_IH_all + exp_maj_wide_IH_all;
gen exp_IRI_IH_SF12a_n_old=	exp_recons_IH_all  + exp_R3_IH_all; 
gen exp_L_IH_SF12a_n_new=	exp_row_IH_all 	   + exp_eng_IH_all	 + exp_new_cons_IH_all 	+ exp_relocation_IH_all	+ exp_maj_wide_IH_all;			
gen exp_IRI_IH_SF12a_n_new=	exp_recons_add_IH  + exp_recons_noadd_IH + exp_R3_min_wide_IH	+ exp_R3_rehab_rest_IH 	+ exp_R3_resurf_IH;


* Do the same for bridge expenditure ;
gen exp_bridge_IH_SF12a_n_new=cond( year>1998,
									exp_new_bridge_IH_all+exp_bridgereplacement_IH_all+exp_majorbridgerehab_IH_all+exp_minorbridgerehab_IH_all,.);
									
gen exp_bridge_IH_SF12a_n_old=cond( year<=1998,
									exp_bridge_IH_all,.);

*diagnostics -- should all be zero -- this test won't work for construction;
sum exp_IRI_IH_SF12a_n_new exp_IRI_IH_SF12a_n_old if year>1998;
sum exp_IRI_IH_SF12a_n_new exp_IRI_IH_SF12a_n_old if year<=1998;

*consolidate pre and post 1998 variable construction;
gen exp_IRI_IH_SF12a_n = 	cond(year>1998, exp_IRI_IH_SF12a_n_new, exp_IRI_IH_SF12a_n_old);
gen exp_L_IH_SF12a_n = 		cond(year>1998, exp_L_IH_SF12a_n_new, exp_L_IH_SF12a_n_old);

gen exp_bridge_IH_SF12a_n = 		cond(year>1998, exp_bridge_IH_SF12a_n_new, exp_bridge_IH_SF12a_n_old);

*clean up -- save reloacation;
gen temp = exp_relocation_IH_all;
gen temp2 =exp_row_IH_all ;
foreach exp_var of local rur_urb_list{;
		drop `exp_var'_all;
		};
		
drop *_new *_old;
rename temp exp_relocation_IH_all_n; 
rename temp2 exp_row_IH_all_n ;
*calculate total IH and SF12a maintenance;
gen exp_IH_total_SF12_n = exp_IH_mtn_all + exp_IH_k_all;
gen exp_IH_mtn_SF12a_n = exp_IH_total_SF12_n - exp_IRI_IH_SF12a_n - exp_L_IH_SF12a_n -exp_bridge_IH_SF12a_n;
sum exp_IH_mtn_SF12a_n, d ;
replace exp_IH_mtn_SF12a_n= cond(exp_IH_mtn_SF12a_n<0,0,exp_IH_mtn_SF12a_n );
sum exp_IH_mtn_SF12a_n, d ;
;

*convert to real 2010 USD 10^6;
*NB: deflate calculated in calling program;
rename app_IH app_IH_n;
rename exp_IH_mtn_all exp_IH_mtn_SF12_n;
ren rev_grand_total_mt rev_grand_total_mt_n;
ren rev_total_h rev_total_h_n;
ren rev_total_mt rev_total_mt_n ;
ren rev_total_m_fuel_h rev_total_m_fuel_h_n ;



local real_list "exp_IH_total_SF12 exp_IH_mtn_SF12a exp_IRI_IH_SF12a exp_L_IH_SF12a app_IH exp_IH_mtn_SF12 exp_relocation_IH_all 
				 rev_grand_total_mt rev_total_h rev_total_mt rev_total_m_fuel_h
				 exp_bridge_IH_SF12a exp_row_IH_all
				 ";
foreach exp_var of local real_list{;
		gen `exp_var'_r=deflate*`exp_var'_n;
		drop `exp_var'_n;
		};

	
forvalues i=1(1)5{;
		
		gen deflate_L`i'= 1/(ppiaco_2010_L`i'*10);
		ren L`i'FHWA_apport_LW L`i'FHWA_apport_LW_n; 
		gen L`i'FHWA_apport_LW_r=deflate_L`i'*L`i'FHWA_apport_LW_n;
		drop L`i'FHWA_apport_LW_n;
};		
		
		
*label and drop superflous expenditure data;
label var 	exp_IH_total_SF12_r 	"Total IH exp SF12 real";
label var 	exp_IH_mtn_SF12_r 	"Total IH maint exp SF12 real";
label var 	exp_IH_mtn_SF12a_r 	"Total IH maint exp SF12a real"; 
label var 	exp_IRI_IH_SF12a_r 	"Total IH IRI exp SF12a real"; 
label var 	exp_L_IH_SF12a_r  	"Total IH L exp SF12a real";
label var 	app_IH_r  		"Total IH approp real";
label var 	exp_relocation_IH_all  	"Total IH exp relocation, >1998";
label var   rev_grand_total_mt_r  	  "Total revenue mass transit account real [source: FE9 p2 ]";
label var   rev_total_mt_r  	  "Total revenue highway and mass transit account real [source: FE9 p2 ]";
label var   rev_total_h  "Total revenue highway account real [source: FE9 p2 ]";
label var rev_total_m_fuel_h              	 "Total Revenue motor fuel  (Highway motor fuel) [source: FE9 p1 ]";	
*drop exp_IH_k_rural-exp_subtotal_mtn_urban;

exit;
